// pages/login/accountLogin/accountLogin.js
const app = getApp()
const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginBtn:false,
    phone:'',
    pwd:'',
    eyes:false,
    focus:false,
    phoneFocus:false,
    del:false,
    checkFlag:false
  },
  look: function(e){
    this.setData({
      eyes:!this.data.eyes,
      focus:true
    })
  },
  clearNum:function(e){
    if (e.detail.value.length > 0){
      this.setData({
        del:true
      })
      if (e.detail.value.length == 11){
        this.setData({
          phone: e.detail.value
        }) 
      }
    }
  },
  cancel:function(e){
    this.setData({
      phone:'',
      checkFlag:true,
      del: false
    })
  },
  phone:function(e){
    setTimeout(()=>{
      if (this.data.checkFlag) {
        this.setData({
          checkFlag: false
        })
        return
      }
      if (e.detail.value.length == '11') {
        this.setData({
          loginBtn: true,
          phone: e.detail.value
        })
      }
      this.setData({
        phone: e.detail.value
      })
      if (this.data.phone == '') {
        wx.showToast({
          title: '请填写手机号',
          image: '/images/register/cuowu@2x.png',
          duration: 2000
        })
        return false
      }
      let check = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(this.data.phone)
      if (!check) {
        wx.showToast({
          title: '手机号错误',
          image: '/images/register/cuowu@2x.png',
          duration: 2000
        })
        return false
      }
      this.setData({
        loginBtn: true,
      })
    },500)
    
  },
  pwd: function (e) {
    // console.log(e)
    this.setData({
      pwd: e.detail.value
    })
  },
  formSubmit: function (e) {
    if (!this.wxValidate.checkForm(e)) {
      wx.showToast({
        title: this.wxValidate.errorList[0].msg,
        image: '/images/register/cuowu@2x.png',
        duration: 2000
      })
      return
    }
    this.login()
  },
  login: function(e){
    // console.log(this.data.phone)
    
    wx.showLoading({
      title: '',
      mask: true,
      success:() => {
        wx.request({
          url: app.globalData.baseUrl +'/WXlogin',
          data: {
            phone: this.data.phone,
            password: this.data.pwd,
            code:app.globalData.code
          },
          method: 'post',
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            wx.hideLoading()
            // console.log(res.data)
            if (res.data.code == '0') {
              wx.setStorageSync('userInfo', res.data.data.user)
              wx.setStorageSync('session', res.data.data.user.session_key)
              wx.setStorageSync('openid', res.data.data.user.openid)
              util.shareFn()
            }else if(res.data.code == '1'){
              wx.navigateTo({
                url: '../../unenter/unenter',
              })
            } else {
              wx.showToast({
                title: res.data.message,
                image: '/images/register/cuowu@2x.png',
                duration: 2000
              })
            }
          }
        })
      }
    })
    
  },
  loginByCode:function(){
    if(this.data.phone.length == '11'){
      let pages = getCurrentPages()
      let prevPage = pages[pages.length - 2]
      prevPage.setData({
        phone: this.data.phone,
        phoneFocus:true
      })
    }
    wx.navigateBack({
      url: '../phoneLogin/phone-login',
    })
  },
  forgot:function(){
    wx.navigateTo({
      url: 'forgot/forgot',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.phone)
    if(options.phone.length == '11'){
      this.setData({
        phone: options.phone,
        phoneFocus:true
      })
    }
    
    this.wxValidate = app.wxValidate(
      {
        mobile: {
          required: true,
          tel: true,
        },
        pwd: {
          required: true,
          minlength: 8
        }
      }, {
        mobile: {
          required: '请填写手机号'
          // tel: 'dsfsdfsdf'
        },
        pwd: {
          required: '请填写密码',
          minlength: '密码错误'
        }
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})